package com.example.prison.config;

import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
   private static final Logger log = LoggerFactory.getLogger(RabbitMqConfig.class.getSimpleName());
   public static final String DIRECT_QUEUE_VOLTAGE = "direct_voltage";
   public static final String DIRECT_EXCHANGE_VOLTAGE = "direct_exchange_voltage";
   public static final String DIRECT_ROUTING_KEY_VOLTAGE = "prison_direct_routing_key_voltage";
   public static final int DEFAULT_CONCURRENT = 10;
   @Autowired
   private CachingConnectionFactory connectionFactory;

   @Bean({"customContainerFactory"})
   public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
      SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
      factory.setConcurrentConsumers(10);
      factory.setMaxConcurrentConsumers(10);
      configurer.configure(factory, connectionFactory);
      return factory;
   }

   @Bean
   public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
      RabbitTemplate rabbitTemplate = new RabbitTemplate();
      rabbitTemplate.setConnectionFactory(connectionFactory);
      rabbitTemplate.setMessageConverter(this.jsonMessageConverter());
      rabbitTemplate.setMandatory(false);
      return rabbitTemplate;
   }

   @Bean
   public Jackson2JsonMessageConverter jsonMessageConverter() {
      return new Jackson2JsonMessageConverter();
   }

   @Bean
   public DirectExchange locationExchange() {
      return new DirectExchange("direct_exchange_voltage", true, false);
   }

   @Bean
   public Queue locationQueue() {
      return new Queue("direct_voltage", true, false, false, (Map)null);
   }

   @Bean
   Binding direcBindingLocation() {
      return BindingBuilder.bind(this.locationQueue()).to(this.locationExchange()).with("prison_direct_routing_key_voltage");
   }
}
